package com.atguigu.yygh.order.listener;

import com.atguigu.yygh.hosp.result.R;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.mapper.OrderInfoMapper;
import com.atguigu.yygh.rabbit.consts.MqConst;
import com.atguigu.yygh.rabbit.service.RabbitService;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ListenerTask {
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    RabbitService rabbitService;

    //订单服务监听第四个队列
    @RabbitListener(
            bindings ={
                    @QueueBinding(
                            value = @Queue(name = MqConst.TASK_QUEUE,durable = "true",declare = "true"),
                            exchange =@Exchange(name = MqConst.TASK_EXCHANGE,durable = "true",declare = "true"),
                            key = MqConst.TASK_KEY
                    )
            }
    )
    public void listenerTaskQueue(String date){  //参数是队列中的消息
        //SELECT * FROM `order_info` WHERE reserve_date='2023-08-22' AND order_status=1
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reserve_date",date);
        queryWrapper.eq("order_status",1);
        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(queryWrapper);

        for (OrderInfo orderInfo : orderInfoList) {
            String patientPhone = orderInfo.getPatientPhone();  //就诊人的手机号
            String message="尚医通"+orderInfo.getPatientName()+"您好!,请在明天"+date+",就诊于"+orderInfo.getHosname()+","+orderInfo.getDepname()+"科室";

            MsmVo msmVo = new MsmVo();
            msmVo.setPhone(patientPhone);
            msmVo.setMessage(message);

            //向消息队列发送消息
            rabbitService.sendMessage(MqConst.MSM_EXCHANGE,MqConst.MSM_KEY,msmVo);
        }

    }
}
